home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / SAT 2.3.7 / Demos / Demo ƒ / StepPlatform Demo ƒ / StepPlatform.p < prev    next >
Encoding:
Text File  |  1995-09-06  |  8.2 KB  |  269 lines  |  [TEXT/PJMM]

  1. { StepPlatform by Nissan Zafrir, based on MyPlatform and StepZkrolly}
  2.  
  3. { Modifications by Ingemar R:}
  4. { Scroll-length timed after TickCount in order to make it fast enough on slow Macs}
  5. { Replaced ox/oy by gSAT.wind.port->portRect.left and .top (ox/oy are obsolete)}
  6. { B/W icons and patterns}
  7. { Centered window}
  8. { Aligned scrolling to multiples of 8 (to account for a limitation in the 1-bit and 4-bit blitters)}
  9.  
  10.  
  11. (*}
  12. {Desirable improvements:}
  13. {• The man should look better}
  14. {• Some kind of opponents?}
  15. {• Ladders?}
  16. {• Better, more general rect-bounce utilities? (I.e. using SATToolbox.)}
  17. {*)
  18.  
  19.  
  20. program StepPlatform;
  21.  
  22.     uses
  23. {$ifc UNDEFINED THINK_PASCAL}
  24.         Types, QuickDraw, Menus, Windows, TextEdit, Fonts, Dialogs, Memory, ToolUtils, {}
  25. {$endc}
  26.         SAT, PlatformGlobals, sPlayerSprite, sPlatForm, sHMovPlatform, sMovPlatform, sEmptyPlatform, InformUser;
  27.  
  28.     var
  29.         thepat: SATPatHandle;
  30.         ignoreSp: SpritePtr;
  31.         p: Point;
  32. {playerSp: SpritePtr;}
  33.         gWind: WindowPtr;
  34.         gVBLInstalled: Integer;
  35.         r: Rect;
  36.  
  37.     const
  38.         scrollSizeH = 512;
  39.         scrollSizeV = 384;
  40.  
  41. {playerSp^.position -> viewPoint}
  42. {scrollSizeH -> ?}
  43. {}
  44. {(Ingemar's private notes)}
  45. {Mer att fixa: Kan ScrollScreen göras så den alltid scrollar direkt, i ett steg?}
  46. {Då skulle "vanlig" scroll fixa sig med samma rutin! Specialfall av scrollSpeed?}
  47. {Nytt namn - SATScroll? Eller om två rutiner, SATScroll och SATStepScroll?}
  48.  
  49.     function ScrollScreen (viewPoint: Point; marginH, marginV, scrollSpeed: Integer): Boolean;
  50. {const}
  51. {sco = 8;        {the speed of the scroll, number of pixels per *tick*}
  52.         var
  53.             startTicks, frameTime, step: LongInt;
  54.             srcRect: Rect;
  55.             where, were: Point;
  56.             nowOff: Point;
  57.             scrollSizeH, scrollSizeV: Integer;
  58.         function max (x, y: Integer): Integer;
  59.         begin
  60.             if x > y then
  61.                 max := x
  62.             else
  63.                 max := y;
  64.         end; {max}
  65.         function min (x, y: Integer): Integer;
  66.         begin
  67.             if x < y then
  68.                 min := x
  69.             else
  70.                 min := y;
  71.         end; {min}
  72.     begin
  73.         scrollSizeH := gSAT.wind.port^.portRect.right - gSAT.wind.port^.portRect.left;
  74.         scrollSizeV := gSAT.wind.port^.portRect.bottom - gSAT.wind.port^.portRect.top;
  75.  
  76.         frameTime := 1;
  77. { If the player sprite is at the border, scroll!}
  78.         if ((viewPoint.h + marginH > scrollSizeH + gSAT.wind.port^.portRect.left) or (viewPoint.h - marginH < gSAT.wind.port^.portRect.left) or (viewPoint.v + marginV > scrollSizeV + gSAT.wind.port^.portRect.top) or (viewPoint.v - marginV < gSAT.wind.port^.portRect.top)) then
  79. {gSAT.wind.port->portRect.left/top = ox/oy!}
  80.             begin
  81.                 SATSetPortScreen;
  82.                 nowOff := gSAT.wind.port^.portRect.topLeft; {Get old origin}
  83.                 were := nowOff;
  84.                 where := viewPoint;
  85.                 where.h := where.h - BSR(scrollSizeH, 1);
  86.                 where.v := where.v - BSR(scrollSizeV, 1);
  87.                 if where.h < 0 then
  88.                     where.h := 0;
  89.                 if where.v < 0 then
  90.                     where.v := 0;
  91.                 if where.h + scrollSizeH > gSAT.offSizeH then
  92.                     where.h := gSAT.offSizeH - scrollSizeH;
  93.                 if where.v + scrollSizeV > gSAT.offSizeV then
  94.                     where.v := gSAT.offSizeV - scrollSizeV;
  95.  
  96.                 where.h := BitAnd(where.h, $fff8); { Scroll only to multiples of 8, so we won't confuse the 4-bit and 1-bit blitters!}
  97.  
  98.                 repeat
  99.                     begin
  100.                         startTicks := TickCount;
  101.  
  102.                         step := scrollSpeed * frameTime;
  103.                         if (nowOff.h > where.h) then
  104.                             nowOff.h := max(nowOff.h - step, where.h);
  105.                         if (nowOff.h < where.h) then
  106.                             nowOff.h := min(nowOff.h + step, where.h);
  107.                         if (nowOff.v > where.v) then
  108.                             nowOff.v := max(nowOff.v - step, where.v);
  109.                         if (nowOff.v < where.v) then
  110.                             nowOff.v := min(nowOff.v + step, where.v);
  111.                         SetOrigin(nowOff.h, nowOff.v);
  112.                         gSAT.wind.bounds := gSAT.wind.port^.portRect; { Synch gSAT.wind.bounds with the portRect!}
  113.                         srcRect := gSAT.wind.port^.portRect;
  114.  
  115.                         CopyBits(gSAT.offScreen.port^.portBits, gSAT.wind.port^.portBits, srcRect, srcRect, srcCopy, nil);
  116.  
  117.                         frameTime := TickCount - startTicks;
  118.                     end;
  119.                 until not ((nowOff.h <> where.h) or (nowOff.v <> where.v));
  120.             end;
  121.     end; {ScrollScreen}
  122.  
  123.     procedure SetupWind;
  124.  
  125.         var
  126.             zr: Rect;
  127.             wrld: SysEnvRec;
  128.  
  129.     begin
  130.     {• Since SAT hasn't been initialized, we can't use gSAT.colorFlag but }
  131.     {• have to check environs ourselves.}
  132.         if (noErr <> SysEnvirons(1, wrld)) then
  133.             ;{• ignore errors.}
  134.  
  135. {    SetRect(&zr, (512-scrollSizeH)/2, 0,  (512-scrollSizeH)/2 + scrollSizeH, 0 + scrollSizeV);}
  136. { no- center on screen instead!}
  137.         SetRect(zr, 0, 0, scrollSizeH, scrollSizeV);
  138. {$IFC UNDEFINED THINK_PASCAL}
  139.         OffsetRect(zr, (qd.screenBits.bounds.right - qd.screenBits.bounds.left - scrollSizeH) div 2, (qd.screenBits.bounds.bottom - qd.screenBits.bounds.top - scrollSizeV) div 2);
  140. {$ELSEC}
  141.         OffsetRect(zr, (screenBits.bounds.right - screenBits.bounds.left - scrollSizeH) div 2, (screenBits.bounds.bottom - screenBits.bounds.top - scrollSizeV) div 2);
  142. {$ENDC}
  143.         if (wrld.hasColorQD) then
  144.             gWind := NewCWindow(nil, zr, '', false, plainDBox, WindowPtr(-1), false, 0)
  145.         else
  146.             gWind := NewWindow(nil, zr, '', false, plainDBox, WindowPtr(-1), false, 0);
  147.     end; {SetupWind}
  148.  
  149.     function IsOptionPressed: Boolean;
  150.         var
  151.             km: KeyMap;
  152.     begin
  153.         GetKeys(km);
  154.         IsOptionPressed := km[56];
  155.     end;
  156.  
  157. {main }
  158.  
  159.     var
  160.         tempRect: Rect;
  161.         e: EventRecord;
  162.         startTicks: Longint;
  163.         where: Point;
  164.  
  165. begin
  166. {$IFC UNDEFINED THINK_PASCAL}
  167.     SATInitToolbox;
  168.     GetDateTime(qd.randSeed);
  169. {$ELSEC}
  170.     GetDateTime(randSeed);
  171. {$ENDC}
  172.  
  173.     SATConfigure(true, kVPositionSort, kBackwardCollision, 64);
  174.     SetupWind;
  175.  
  176.     SetRect(r, 0, 0, 1000, 484);    {the offscreen size}
  177.     SATCustomInit(0, 0, r, gWind, nil, false, false, false, true, false);
  178.     SATSetSpriteRecSize(sizeof(Sprite));
  179.  
  180.     ShowWindow(gSAT.wind.port);
  181.     SelectWindow(gSAT.wind.port);
  182.     SATHideMBar(gWind);
  183.  
  184.     (* fill the backscreen in a pattren *)
  185.     SATSetPortBackScreen;
  186.     if (IsOptionPressed) then
  187.         thepat := SATGetPat(SATRand(5) + 128)    { choose a Pat in Random}
  188.     else
  189.         thepat := SATGetPat(128);{128-brown wall, 130-gray wall pattren}
  190.     SATPenPat(thepat);
  191.     SetRect(tempRect, 0, 0, gSAT.offSizeH, gSAT.offSizeV + 100);
  192.     PaintRect(tempRect);
  193.     CopyBits(gSAT.backScreen.port^.portBits, gSAT.offScreen.port^.portBits, gSAT.offScreen.port^.portRect, gSAT.offScreen.port^.portRect, srcCopy, nil);
  194.     SATBackChanged(gSAT.offScreen.port^.portRect);
  195.     SATRedraw;
  196.  
  197.     (*Initialize all sprite units*)
  198.     InitPlayerSprite;
  199.     InitPlatform;
  200.     InitEmptyPlatform;
  201.     InitMovPlatform;
  202.     InitHMovPlatform;
  203.     InitInformationArea;
  204.  
  205.     SATRedraw;
  206.  
  207.     (* SetUp my Sprites    *)
  208.     GetMouse(p);
  209.     playerSp := PlSpritePtr(SATNewSprite(1, p.h, p.v, @SetupPlayerSprite));    { Keep Player Sprite}
  210.     ignoreSp := SATNewSprite(0, 0, 350, @SetupEmptyPlatform);    { the Floor Platform}
  211.     SetRect(ignoreSp^.hotRect, 0, 0, gSAT.offSizeH - 150, 20);
  212.     ignoreSp := SATNewSprite(0, 55, 300, @SetupPlatform);        { Standing Platform}
  213.     ignoreSp := SATNewSprite(0, 245, 235, @SetupPlatform);        { Standing Platform}
  214.     ignoreSp := SATNewSprite(55, 355, 200, @SetupMovPlatform);    {50=MinV    200=MaxV}
  215.         {ignoreSp->position.h=30;    // I can change the defeults}
  216.     ignoreSp := SATNewSprite(100, 55, 200, @SetupMovPlatform);    {100=MinV    230=MaxV}
  217.     ignoreSp := SATNewSprite(128, 270, 120, @SetupHMovPlatform);    {120=MinH   150=MaxH}
  218.     ignoreSp^.speed.h := 2;
  219.  
  220.     ignoreSp := SATNewSprite(250, 850, 400, @SetupMovPlatform);    {50=MinV    200=MaxV}
  221.  
  222.     ignoreSp := SATNewSprite(55, 700, 250, @SetupMovPlatform);        {50=MinV    200=MaxV}
  223.     ignoreSp := SATNewSprite(430, 630, 85, @SetupHMovPlatform);    {50=MinV    200=MaxV}
  224.  
  225.     (*Update the game window once more, so the pattern and what we drawn in DrawInfo are shown. *)
  226.     SATRedraw;
  227.  
  228.     HideCursor;
  229.     SATSoundOff;
  230.  
  231.     if (gSAT.colorFlag) then { Can't do SlotVInstall without CQD}
  232.         if (IsOptionPressed) then
  233.             begin
  234. {    InstallVBLCounter;}
  235. {    SATInstallSynch(VBLCounterProc);    {WaitForSync}
  236. {    SysBeep(7);}
  237. {    gVBLInstalled := true;}
  238.             end;
  239.  
  240. (* the main loop *)
  241.  
  242.     gSAT.wind.bounds := gSAT.wind.port^.portRect;
  243.     SATSetPortScreen;
  244.     SATRedraw;
  245.     repeat
  246.         startTicks := TickCount; {If not VBL-synched}
  247.         SATRun(true);    {!IsOptionPressed()}
  248.         where.h := playerSp^.position.h + 32;
  249.         where.v := playerSp^.position.v + 32;
  250.         if ScrollScreen(where, 32, 32, 8) then
  251.             ;
  252.         if IsOptionPressed then
  253.             DrawProgrammerInfo;
  254. {if (IsOptionPressed()) if (WaitNextEvent ( everyEvent, &e, 10, nil )) ;}
  255.  
  256.         while TickCount < startTicks + 1 do
  257.             ; {Max 60 fps}
  258.     until Button;
  259.  
  260. (* cleanning up *)
  261.     SATSetPortScreen;
  262. {    if gVBLInstalled then}
  263. {    RemoveVBLCounter;}
  264.     SATSoundShutup;
  265.     SATShowMBar(gWind);
  266.  
  267.     ShowCursor;
  268.     FlushEvents(everyEvent, 0);
  269. end. {main}